package com.zhiwang.planet.mvvm.viewmodel

import android.content.Intent
import android.view.View
import com.zhiwang.planet.event.TabChangeEvent
import com.zhiwang.planet.mvvm.viewmodel.main.sharedViewModels
import com.zhiwang.planet.ui.MembershipActivity
import com.zhiwang.planet.ui.WebViewActivity
import com.zhiwang.planet.ui.entertain.MiniGameActivity
import com.zhiwang.planet.ui.gamebase.GameBaseActivity
import com.zhiwang.planet.ui.my.MyDetailActivity
import com.zhiwang.planet.ui.my.WithDrawActivity2
import com.zhiwang.planet.ui.my.withdraw.WithDrawRecordFragment
import com.zhiwang.planet.util.AppUtil
import com.zhiwang.planet.util.TokenManager
import org.greenrobot.eventbus.EventBus

class HeaderRecommendItemViewModel(
        val type: Int,
        val title: String,
        val description: String,
        val iconRes: Int,
        val isRecomm: Boolean,
        val link: String? = null,
        var iconUrl: String = "",
        var native: Int?,
        var nativePageId: Int?
        /*超链接等等属性*/) {

    val shareViewModel by sharedViewModels<ShareViewModel>()

    fun onItemClick(v: View) {

        if (type == 51) {
            shareViewModel.vipAct.value = true
            return
        }

        //原生页面跳转
        if (native == 1) {
            val intent = Intent()
            when (nativePageId) {
                //游戏基地页面
                5 -> intent.setClass(v.context, GameBaseActivity::class.java)
                //提现页面
                6 -> intent.setClass(v.context, WithDrawRecordFragment::class.java)
                //我的明细
                7 -> intent.setClass(v.context, MyDetailActivity::class.java)
                //vip 页面
                8 -> intent.setClass(v.context, MembershipActivity::class.java)

                15 -> intent.setClass(v.context, MiniGameActivity::class.java)
                16 -> intent.setClass(v.context,WithDrawActivity2::class.java)
                else -> return
            }

            v.context.startActivity(intent)

            return
        }


        //之前的 h5页面或者是 首页tab切换

//        if (type == 3) {//type 3 固定是活动 ，且不需要登录
//            WebViewActivity.load(v.context, Store.URL_ACTIVITY)
//            return
//        }

        //其他页面切换需要登录
        if (TokenManager.isLoginValid().not()) {
            AppUtil.onAuthExpired(v.context)
            return
        }
        if (link != null && link.isNotEmpty()) {
            WebViewActivity.loadImmersive(v.context, link)
            return
        }
        when (type) {
            2, 3 -> EventBus.getDefault().post(TabChangeEvent(2))
            4 -> EventBus.getDefault().post(TabChangeEvent(1))
        }
    }
}